<template>
	<view>
		<!-- 导航栏 -->
		<cu-custom bgColor="bg-gradual-red" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">文件数据编辑</block>
		</cu-custom>
		<form>
			<view class="cu-form-group margin-top-sm">
				<view class="title text-bold">文件标题</view>
				<input class="" placeholder="文件标题" name="fileTitle" v-model="fileTitle"></input>
			</view>
			<view class="cu-form-group">
				<view class="title text-bold">共享类型</view>
				<!-- <picker @change="pickerChange" :value="fileNatureIndex" :range="fileNatures" range-key="name">
					<view class="picker" style="text-align: left;">
						{{fileNatureName}}
					</view>
				</picker> -->
				<radio-group class="block flex1 fontsmallsize" @change="fileNatureChange">
					<label class="radio margin-right-sm" v-for="(item,index) in fileNatures" :key="index"><radio :class="item.id==fileNature?'checked':''" class="margin-right-xs" name="fileNature" :value="item.id" />{{item.name.length>4?item.name.substr(0,4):item.name}}</label>
				</radio-group>
			</view>
			<view class="cu-form-group">
				<view class="title text-bold">文件种类</view>
				<!-- <picker mode="selector" :range="fileTypes" :value="fileTypeIndex" @change="fileTypeChange" range-key="name">
					<view class="picker" style="text-align: left;">{{fileTypeName}}</view>
				</picker> -->
				<radio-group class="block flex1 fontsmallsize" @change="fileTypeChange">
					<label class="radio margin-right-sm" v-for="(item,index) in fileTypes" :key="index"><radio :class="item.id==fileType?'checked':''" class="margin-right-xs" name="fileType" :value="item.id"  />{{item.name.length>4?'文档':item.name}}</label>
				</radio-group>
			</view>
			<view class="cu-form-group align-start margin-top-xs">
				<view class="title text-bold">文件说明</view>
				<textarea v-model="fileRemark" maxlength="-1"  placeholder="文件详细说明"></textarea>
			</view>
			<view class="cu-bar bg-white margin-top-xs">
				<view class="action text-bold">图片上传</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-grey lg" @tap="to_save">提交</button>
				<!-- <button class="cu-btn bg-red margin-tb-sm lg">嫣红</button> -->
			</view>
			
		</form>
	</view>
</template>

<script>
	//import { mapState } from 'vuex';
	
	export default {
		computed:{
			userName(){
				return this.$store.state.userName;
			},
			pwd(){
				return this.$store.state.pwd;
			},
			fileNatureName(){
				return this.fileNatureIndex>-1?this.fileNatures[this.fileNatureIndex].name:'--请选择--';
			},
			fileTypeName(){
				return this.fileTypeIndex>-1?this.fileTypes[this.fileTypeIndex].name:'--请选择--';
			},
			hasLogin(){
				return this.$store.state.hasLogin;
			},
			server(){
				return this.$store.state.server;
			}
		},
		data() {
			return {
				preIndex:0,
				id:'',
				file2:null,
				fileTitle:'',
				fileNature:'',
				fileType:'',
				fileRemark:'',
				fileNatures:[{id:0,name:'aaa'}],
				fileNatureIndex:0,
				fileTypes:[{id:0,name:'bbb'}],
				fileTypeIndex:0,
				imgList:[]
			}
		},
		methods:{
			pickerChange(e){
				this.fileNatureIndex=e.detail.value;
				this.fileNature=this.fileNatures[this.fileNatureIndex].id;
			},
			fileNatureChange(e){
				this.fileNature=e.detail.value;
			},
			fileTypeChange(e){
				// this.fileTypeIndex=e.detail.value;
				// this.fileType=this.fileTypes[this.fileTypeIndex].id;
				this.fileType=e.detail.value;
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '对话框',
					content: '确定要删除这张图片吗？',
					cancelText: '退出',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			ChooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			// end_ChooseImage
			to_save(){
				if(!this.fileTitle){
					uni.showModal({
						title: '警告',
						content: '文件标题不能为空',
						showCancel: false
					});
					return;
				}
				var serverUrl=this.server.url;
				var count=0;
				uni.showLoading({
					title: '上传中...',
					mask: true
				});
				this.imgList.map(item=>{
					uni.uploadFile({
						url: serverUrl + 'app/save', 
						filePath:item,
						name:'file',
						formData: {
							id:this.id,
							userName:this.userName,
							pwd:this.pwd,
							fileTitle:this.fileTitle,
							fileRemark:this.fileRemark,
							fileNature:this.fileNature,
							fileType:this.fileType
						},
						complete: () => {
							count++
							if(count==this.imgList.length){
								var pages=getCurrentPages();
								var prePage=pages[pages.length-2];
								prePage.$vm.isBack=true;
								prePage.$vm.backParam={
									method:'edit',
									id:this.id,
									index:this.preIndex
								};
								uni.hideLoading();
								uni.navigateBack();
							}
						}
					});
				});
				if(this.imgList.length<1){
					var postdata={
						id:this.id,
						userName:this.userName,
						pwd:this.pwd,
						fileTitle:this.fileTitle,
						fileRemark:this.fileRemark,
						fileNature:this.fileNature,
						fileType:this.fileType
					};
					uni.request({
						url: serverUrl + 'app/save',
						method: 'POST',
						sslVerify: false,
						header: {  
							'Content-type':'application/x-www-form-urlencoded;charset=utf-8'  
						},  
						data:postdata,
						success: res => {
							var pages=getCurrentPages();
							var prePage=pages[pages.length-2];
							prePage.$vm.isBack=true;
							prePage.$vm.backParam={
								method:'edit',
								id:this.id,
								index:this.preIndex
							};
							uni.hideLoading();
							uni.navigateBack();
						},
						fail: res=>{
							uni.hideLoading();
							uni.showToast({
								title:'网络连接失败，请检查网络...',
								icon: 'none'
							});
						}
					});
				}
			} 
			// end_toSave
		},
		//end_Methods
		onLoad(e) {
			if(!this.hasLogin){
				uni.redirectTo({
					url:'./login'
				});
				return;
			}
			
			var serverUrl=this.server.url;
			var fileid=e.fileid;
			this.preIndex=e.index;
			uni.request({
				url: serverUrl+ 'app/edit',
				method: 'GET',
				sslVerify: false,
				data:{id:fileid},
				success: res => {
					var result=res.data;
					this.fileNatures=result.fileNatures;
					this.fileTypes=result.fileTypes;
					var sysFile=result.sysFile;
					this.id=sysFile.id;
					this.fileTitle=sysFile.fileTitle;
					this.fileNature=sysFile.fileNature.id;
					this.fileType=sysFile.fileType.id;
					this.fileRemark=sysFile.fileRemark;
					this.fileNatureIndex=this.fileNatures.findIndex(item=>{
						return item.id==this.fileNature;
					});
					this.fileTypeIndex=this.fileTypes.findIndex(item=>{
						return item.id==this.fileType;
					});
				}
			});
			
		}
	}
</script>
	
<style>
 .flex1 {
	 flex:1;
 }
 .fontsmallsize {
	 font-size: 13px;
 }
	
</style>
